#include "MCGripPoint.h"
#include <MCSettings.h>
#include <iostream>

MCGripPoint::MCGripPoint(MCGraphicsItem *owner, MCGripPointType type, size_t index, const MCPoint2D &position) :
    MCGraphicsItem(owner->canvas()),
    _owner(owner),
    _index(index),
    _type(type),
    _position(position)
{
    setFillColor(MCColors::Black);
}

void MCGripPoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    setPainter(painter);

    MCPoint2D point = presenter->screenToWorld(_position);
    drawPoint(point);
}

QPainterPath MCGripPoint::shape() const
{
    QPainterPath path;
    MCPoint2D mcPoint = presenter->screenToWorld(_position);
    double width = MCSettings::GripPointWidth;
    QRectF rect(mcPoint.x - width / 2, mcPoint.y - width / 2, width, width);
    path.addRect(rect);

    return path;
}

size_t MCGripPoint::index() const
{
    return _index;
}

IGraphicsItem *MCGripPoint::owner()
{
    return _owner;
}

const MCPoint2D &MCGripPoint::position()
{
    return _position;
}

void MCGripPoint::move(const MCVector2 &direction)
{
    _position = _position + direction;
    _owner->setGripAt(this, _position);
}

void MCGripPoint::setPositionAt(const MCPoint2D &at)
{
    _owner->setGripAt(this, at);
}

void MCGripPoint::update(const MCPoint2D &position)
{
    _position = position;
}

void MCGripPoint::update(double x, double y)
{
    _position = MCPoint2D(x, y);
}

ICanvas *MCGripPoint::canvas()
{
    return _owner->canvas();
}
